package com.beiing.tianshuai.tianshuai.easeui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.easeui.runtimepermissions.PermissionsManager;

public class EaseChatActivity extends BaseActivity {

    public static EaseChatActivity activityInstance;
    private ChatFragment chatFragment;
    String toChatUsername;
    String toChatAvatar;
    String toChatNickName;
    int chatType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ease_chat;
    }

    @Override
    protected void init() {
        activityInstance = this;
        //get user id or group id
        Intent intent = getIntent();
        toChatUsername = intent.getStringExtra("userId");
        toChatAvatar = intent.getStringExtra("toAvatar");
        // TODO: 2018/1/16 0016 根据聊天类型设置昵称
//        chatType= intent.getIntExtra("chatType", EaseConstant.CHATTYPE_SINGLE);
//        if (chatType == EaseConstant.CHATTYPE_SINGLE) {
//            toChatNickName = SPUtils.getString(this,
//                    "nickname" + toChatUsername,
//                    intent.getStringExtra("userNickname"));
//        } else if (chatType == EaseConstant.CHATTYPE_GROUP) {
//            toChatNickName = intent.getStringExtra("userNickname");
//        }
        //use EaseChatFratFragment
        chatFragment = new ChatFragment();
        //pass parameters to chat fragment
        // TODO: 2018/1/16 0016 传递用户属性给下一层
//        Bundle args = new Bundle();
//        args.putInt(EaseConstant.EXTRA_CHAT_TYPE, chatType);
//        args.putString(EaseConstant.EXTRA_USER_ID, toChatUsername);
//        args.putString(EaseConstant.EXTRA_USER_NICKNAME, toChatNickName);
//        args.putString(EaseConstant.EXTRA_USER_AVATAR, toChatAvatar);
//        chatFragment.setArguments(args);
//        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        //get user id or group id
        toChatUsername = getIntent().getStringExtra("userId");
        toChatAvatar = getIntent().getStringExtra("toAvatar");
//        chatType= getIntent().getIntExtra("chatType", EaseConstant.CHATTYPE_SINGLE);
//        if (chatType == EaseConstant.CHATTYPE_SINGLE) {
//            toChatNickName = SPUtils.getString(this,  "nickname" + toChatUsername, toChatUsername);
//        } else if (chatType == EaseConstant.CHATTYPE_GROUP) {
//            toChatNickName = getIntent().getStringExtra("userNickname");
//        }
        //use EaseChatFratFragment
        chatFragment = new ChatFragment();
        //pass parameters to chat fragment
        Bundle args = new Bundle();
//        args.putInt(EaseConstant.EXTRA_CHAT_TYPE, chatType);
//        args.putString(EaseConstant.EXTRA_USER_ID, toChatUsername);
//        args.putString(EaseConstant.EXTRA_USER_NICKNAME, toChatNickName);
//        args.putString(EaseConstant.EXTRA_USER_AVATAR, toChatAvatar);
//        chatFragment.setArguments(args);
//        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }

    @Override
    public void onBackPressed() {
//        if (EaseChatRowImage.isImgFragmentShow) {
//            EaseChatRowImage.hidePreviewIdCardFragment();
//            return;
//        }
//        if (EasyUtils.isSingleActivity(this)) {
//            Intent intent = new Intent(this, MainActivity.class);
//            startActivity(intent);
//        }
//        chatFragment.onBackPressed();
    }

    public String getToChatUsername(){
        return toChatUsername;
    }

    @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                                     @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}
